package com.lihua.newideas.handler;

import com.lihua.newideas.pojo.MyUser;
import com.lihua.newideas.service.MyUserService;
import com.lihua.newideas.service.impl.MyUserServiceImpl;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/*
登录成功处理器
* */

public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler {

    private String url;

    private MyUserService userService;

    public MyAuthenticationSuccessHandler(String url){
        this.url=url;
    }

    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
                                        Authentication authentication) throws IOException, ServletException {
        User user = (User) authentication.getPrincipal();  //这里的User是security的

//        System.out.println(user.getUsername());//获取用户名
//        System.out.println("++++++++++");
//        MyUser myUser = userService.queryUserByName(user.getUsername());
//        if (myUser!=null){
//            myUser.setPassWord("");
//            httpServletRequest.getSession().setAttribute("MyUser",myUser);
//        }



//        System.out.println(user.getPassword());//密码为了安全security吧这个get设置为了空
//        System.out.println(user.getAuthorities()); //获取用户权限
        httpServletRequest.getSession().setAttribute("user",user);

        httpServletResponse.sendRedirect(url);
    }
}
